<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" :request-api="getTableList" :init-param="initParam">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button type="primary" :icon="CirclePlus" @click="openDrawer('新增')"> 新增 </el-button>
        <el-button type="primary" :icon="Download" plain @click="downloadFile">导出数据</el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button type="primary" link :icon="View" @click="openDrawer('查看', scope.row)">查看</el-button>
        <el-button type="primary" link :icon="EditPen" @click="openDrawer('编辑', scope.row)">编辑</el-button>
        <el-button type="primary" link :icon="Delete" @click="deleteAccount(scope.row)">删除</el-button>
      </template>
    </ProTable>
    <DetailDrawer ref="drawerRef" />
    <ImportExcel ref="dialogRef" />
  </div>
</template>

<script setup lang="tsx" name="useProTable">
import { ref, reactive } from "vue";
import { Message } from "@/api/types/base";
import { useHandleData } from "@/hooks/useHandleData";
import { useDownload } from "@/hooks/useDownload";
import { ElMessageBox } from "element-plus";
import ProTable from "@/components/ProTable/index.vue";
import ImportExcel from "@/components/ImportExcel/index.vue";
import DetailDrawer from "./detailDrawer.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { CirclePlus, Delete, EditPen, Download, View } from "@element-plus/icons-vue";
import apiService from "@/api/services/apiService";
import { useDateFormat } from "@vueuse/core";
import { dateRender } from "@/hooks/useColumnRender";

const initVar = reactive({
  tbName: "message"
});

// ProTable 实例
const proTable = ref<ProTableInstance>();

const initParam = reactive({
  // user_id: userStore.userInfo._id
});

const getTableList = (params: any) => {
  return apiService.findPage(initVar.tbName, params.condition, params.options);
};

// 表格配置项
const columns = reactive<ColumnProps<Message>[]>([
  { type: "index", label: " ", width: 85 },
  { prop: "title", label: "标题", width: 200, search: { el: "input" } },
  { prop: "summary", label: "内容" },
  {
    prop: "type",
    label: "类型",
    width: 100,
    // 字典请求携带任务
    enum: () => apiService.findClassify("MESSAGE_TYPE"),
    search: { el: "select", props: { filterable: true } },
    fieldNames: { label: "name", value: "value" }
  },
  {
    prop: "state",
    label: "状态",
    width: 100,
    tag: true,
    // 字典请求携带任务
    enum: () => apiService.findClassify("DATA_STATE"),
    search: { el: "select", props: { filterable: true } },
    fieldNames: { label: "name", value: "value" }
  },
  {
    prop: "create_at",
    label: "创建时间",
    width: 120,
    render: dateRender
  },
  { prop: "operation", label: "操作", fixed: "right", width: 240 }
]);

// 删除用户信息
const deleteAccount = async (params: Message) => {
  await useHandleData(apiService.deleteById, initVar.tbName, params._id, `删除任务【${params.title}】`);
  proTable.value?.getTableList();
};

// 导出用户列表
const downloadFile = async () => {
  ElMessageBox.confirm("确认导出数据?", "温馨提示", { type: "warning" }).then(() =>
    useDownload(
      apiService.export,
      `任务列表-${useDateFormat(new Date(), "YYYY-MM-DD").value}`,
      "task",
      proTable.value?.searchParam
    )
  );
};

// 打开 drawer(新增、查看、编辑)
const drawerRef = ref<InstanceType<typeof DetailDrawer> | null>(null);
const openDrawer = (title: string, row: Partial<Message> = {}) => {
  const params = {
    title,
    tbName: initVar.tbName,
    isView: title === "查看",
    row: row,
    api: title.indexOf("新增") !== -1 ? apiService.save : title === "编辑" ? apiService.updateById : undefined,
    getTableList: proTable.value?.getTableList
  };
  drawerRef.value?.acceptParams(params);
};
</script>
